package com.chocho.producerserver.dao;

import com.chocho.producerserver.bean.Module;
import org.apache.ibatis.annotations.*;
import org.bouncycastle.math.raw.Mod;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by chocho on 2022/3/27.
 */
@Mapper
@Repository
public interface ModuleDao{

    @Select("<script> " +
            "SELECT FROM op_module WHERE module_id in " +
            "<foreach collection='ids' open='(' item='id' separator=',' close=')'> #{id}</foreach> " +
            "</script>")
    List<Module> selectModuleList(List<String> ids);

    @Select("SELECT * FROM op_module WHERE module_id = #{id}")
    Module selectByID(String id);

    @Select("SELECT MAX(moduleOrderNum) FROM op_module WHERE module_parent_id = #{parentID}")
    Integer getMaxSort(String parentID);

    @Insert("INSERT INTO op_module " +
            "<trim prefix='(' suffix=')' suffixOverrides=',' > " +
            "<if test='moduleId != null'>module_id, </if> " +
            "<if test='moduleUrl != null' >module_url, </if>" +
            "<if test='moduleParentid != null' >module_parentid, </if>" +
            "<if test='moduleStatus != null' >module_status, </if>" +
            "<if test='moduleNum != null' >module_num, </if>" +
            "<if test='moduleName != null' >module_name, </if>" +
            "<if test='moduleComponent != null' >module_component, </if>" +
            "<if test='moduleRedirect != null' >module_redirect, </if>" +
            "</trim> " +
            "<trim prefix='values (' suffix=')' suffixOverrides=',' > " +
            "<if test='moduleId != null' > #{moduleId,jdbcType=CHAR}, </if>" +
            "<if test='moduleUrl != null' > #{moduleUrl,jdbcType=VARCHAR}, </if>" +
            "<if test='moduleParentid != null' > #{moduleParentid,jdbcType=CHAR}, </if>" +
            "<if test='moduleStatus != null' > #{moduleStatus,jdbcType=TINYINT}, </if>" +
            "<if test='moduleNum != null' > #{moduleNum,jdbcType=INTEGER}, </if>" +
            "<if test='moduleName != null' > #{moduleName,jdbcType=VARCHAR}, </if>" +
            "<if test='moduleComponent != null' > #{moduleComponent,jdbcType=VARCHAR}, </if>" +
            "<if test='moduleRedirect != null' >#{moduleRedirect,jdbcType=VARCHAR}, </if>" +
            "</trim>")
    Integer insertNotNull(Module module);

    @Update("UPDATE op_module " +
            "<set> " +
            "<if test='moduleId != null'> module_id = #{moduleId,jdbcType=CHAR}, </if> " +
            "<if test='moduleUrl != null'> module_url = #{moduleUrl,jdbcType=VARCHAR}, </if> " +
            "<if test='moduleParentid != null'> module_parentid = #{moduleParentid,jdbcType=CHAR}, </if> " +
            "<if test='moduleStatus != null'> module_status = #{moduleStatus,jdbcType=TINYINT}, </if> " +
            "<if test='moduleNum != null'> module_num = #{moduleNum,jdbcType=INTEGER}, </if> " +
            "<if test='moduleName != null'> module_name = #{moduleName,jdbcType=VARCHAR}, </if> " +
            "<if test='moduleComponent != null'> module_component = #{moduleComponent,jdbcType=VARCHAR}, </if> " +
            "<if test='moduleRedirect != null'> module_redirect = #{moduleRedirect,jdbcType=VARCHAR}, </if> " +
            "</set> " +
            "WHERE module_id = #{moduleId,jdbcType=CHAR} ")
    Integer updateByIDNotNUll(Module module);

    @Delete("DELETE FROM op_module WHERE module_id = #{id}")
    Integer deleteByID(String id);

    @Select("SELECT * FROM op_module WHERE module_name = #{name}")
    List<Module> selectByName(String name);

    @Select("SELECT * FROM op_module ORDER BY moduleNum ASC")
    List<Module> selectAlls();

    @Select("SELECT * FROM op_module WHERE module_parentid = #{parentid} and module_status != '0'")
    List<Module> selectByParent(String parentid);
}
